package com.jiuzhe.commercial.ui.my;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.blankj.utilcode.utils.RegexUtils;
import com.jiuzhe.commercial.R;
import com.jiuzhe.commercial.base.BaseActivity;
import com.jiuzhe.commercial.constant.Constants;
import com.jiuzhe.commercial.http.CallBack;
import com.jiuzhe.commercial.http.HttpClient;
import com.jiuzhe.commercial.utils.SPUtil;
import com.jiuzhe.commercial.utils.UIUtils;
import com.jiuzhe.commercial.view.TimeButton;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by huanghao on 2018/5/25.
 */

public class FindPassWordActivity extends BaseActivity implements TextWatcher {
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.tbn_get_code)
    TimeButton tbnGetCode;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.bt_next)
    Button btNext;
    private String mPhone,mCode,rCode;


    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.activity_find_password);
        setTitle("找回支付密码");
    }

    @Override
    protected void loadData() {
        etPhone.addTextChangedListener(this);
    }

    @OnClick({R.id.bt_next,R.id.tbn_get_code})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_next:
                nextStep();

                break;
            case R.id.tbn_get_code:
                getCode();
                break;
        }
    }

    /**
     * 获取短信验证码
     */
    private void getCode() {
        int numcode = (int) ((Math.random() * 9 + 1) * 100000);
        rCode = String.valueOf(numcode);
        mCode = etCode.getText().toString().trim();
        HashMap<String, String> map = new HashMap<>();
        map.put("phone", etPhone.getText().toString().trim());
        map.put("messageCode", String.valueOf(numcode));


        HttpClient.post(this, Constants.PHONE_CODE, map, new CallBack<String>() {
            @Override
            public void onSuccess(String s) {
                if (mCode.equals(String.valueOf(rCode))) {
                    tbnGetCode.setEnabled(true);
                   // etCode.setText(rCode);
                }
            }
        });
    }


    /**
     * 下一步
     */
    private void nextStep() {
        mPhone = etPhone.getText().toString().trim();
        mCode = etCode.getText().toString().trim();
        if (!RegexUtils.isMobileExact(mPhone)) {
            Toast.makeText(this, "请输入正确的手机号码", Toast.LENGTH_SHORT).show();
            return;
        }

        if (TextUtils.isEmpty(mCode)) {
            UIUtils.showToastSafe("请输入短信验证码");
            return;
        }
        UIUtils.startActivity(ResetPayPassWorodActivity.class);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        Constants.PHONE = s.toString().trim();
        if (s.toString().trim().length() == 11) {
            SPUtil.put(this, Constants.User_Phone, s.toString().trim());
        }
    }
}
